{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from SubFunc import printType"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'printType' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[1], line 4\u001b[0m\n\u001b[1;32m      1\u001b[0m a \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mdict\u001b[39m()  \u001b[38;5;66;03m# 定义一个字典变量\u001b[39;00m\n\u001b[1;32m      3\u001b[0m b \u001b[38;5;241m=\u001b[39m {\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mk0\u001b[39m\u001b[38;5;124m\"\u001b[39m: \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mv0\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;241m2\u001b[39m: \u001b[38;5;241m2\u001b[39m, \u001b[38;5;28;01mTrue\u001b[39;00m: \u001b[38;5;28;01mFalse\u001b[39;00m}  \u001b[38;5;66;03m# 键可以多个类型\u001b[39;00m\n\u001b[0;32m----> 4\u001b[0m \u001b[43mprintType\u001b[49m(b)\n",
      "\u001b[0;31mNameError\u001b[0m: name 'printType' is not defined"
     ]
    }
   ],
   "source": [
    "a = dict()  # 定义一个字典变量\n",
    "\n",
    "b = {\"k0\": \"v0\", 2: 2, True: False}  # 键可以多个类型\n",
    "printType(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b[\"k1\"] = 1  # 直接写一个不存在的键,并赋值即可\n",
    "b.update({\"k2\": 2})  # 用Update方法添加另一个字典,可一次添加多个键值对\n",
    "b.setdefault(\"k3\", 3)  # 该方法只能在键不存在的时候自动增加一个默认的键值对\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "del b[\"k3\"]\n",
    "print(b)\n",
    "\n",
    "b.pop(\"k2\")  # pop既可以取,也可以删\n",
    "print(b)\n",
    "c = b.pop(\"k6\", \"报错时的默认值\")  # 用pop 删除一个不存在的键时会报错,需要用try\n",
    "print(b)\n",
    "print(c)\n",
    "\n",
    "\n",
    "b.popitem()  # popitem会删除最后一项\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b[\"k0\"] = 33  # 甚至可以改值的类型\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d = b[\"k0\"]\n",
    "print(d)\n",
    "d = b.get(2)\n",
    "print(d)\n",
    "d = b.get(4, \"报错时的默认值\")\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "e = b.keys()  # 可迭代对象\n",
    "print(e)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "e = b.values()  # 可迭代对象\n",
    "print(e)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "e = b.items()  # 可迭代\n",
    "print(e)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = len(b)\n",
    "print(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字典\n",
    "b = set([1, 1, 2, 2, 3, 3])  # 可用set方法将list转为set;重复的部分会被合并\n",
    "printType(b)\n",
    "c = {2, 3, 4}  # 也可用大括号定义\n",
    "printType(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d = b | c\n",
    "printType(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d = b - c\n",
    "printType(d)\n",
    "d = c - b\n",
    "printType(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d = b & c\n",
    "printType(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d = b ^ c\n",
    "printType(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d = {1, 2, 3} <= {1, 2, 3, 4}  # 左是右的子集\n",
    "print(d)\n",
    "d = {0, 1, 2, 3} <= {1, 2, 3, 4}  # 右边未完全包含左边\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = {1, 2, 3}\n",
    "f.update({3, 4})  # 可以通过合并另一个集合的形式，增加元素\n",
    "print(f)\n",
    "f.add(5)\n",
    "print(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f.remove(1)\n",
    "print(f)\n",
    "try:\n",
    "    f.remove(6)  # 删除一个不存在的元素时会报错,最好用try语句包裹\n",
    "    print(f)\n",
    "except Exception as e:\n",
    "    print(e)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
